﻿using System;
using System.Collections.Generic;
using System.Text;
using MINERP.MODEL.EM;
using MINERP.DAL.EM;

namespace MINERP.BLL.EM
{
    public class Users
    {
        //public static object Register(UsersInfo entity)
        //{
        //    using(UsersDAL dal=new UsersDAL())
        //    {
        //     return  dal.Register(entity);
        //    }
        //}

        public static UsersInfo Login(int loginID, string password)
        {
            using (UsersDAL dal = new UsersDAL())
            {
                UsersInfo obj = dal.Select(loginID);
                if (obj.LoginPwd == password)
                    return obj;
                else
                    return null;
            }
        }

        public  static UsersInfo Select(int id)
        {
            using (UsersDAL dal = new UsersDAL())
            {
                return dal.Select(id);
            }
        }

        public static List<UsersInfo> SelectAll()
        {
            using(UsersDAL dal = new UsersDAL())
            {
                return dal.SelectAll();
            }
        }
        //public static List<UsersInfo> Search(UsersInfo entity)
        //{
        //   using(UsersDAL dal=new UsersDAL())
        //    {
        //       return  dal.Search(entity);
        //    }
        //}

        /// <summary>
        /// 根据条件组合来搜索
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="nickName"></param>
        /// <param name="ageBound"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        //public static List<UsersInfo> Search(int userId,string nickName,string ageBound,string sex)
        //{
        //    int iFromAge = 0;
        //    int iToAge = 0;

        //    if (!string.IsNullOrEmpty(ageBound))
        //    {
        //      string[] strAge= ageBound.Split('~');
        //      if (strAge.Length == 2)
        //      {
        //          iFromAge = int.Parse(strAge[0]);
        //          iToAge = int.Parse(strAge[1]);
        //      }
        //      else
        //      {
        //          if (ageBound.Substring(0, 1).Equals(">"))
        //              iFromAge = int.Parse(ageBound.Substring(1));
        //          else
        //              iToAge = int.Parse(ageBound.Substring(1));
        //      }
        //    }

        //    using (UsersDAL dal = new UsersDAL())
        //    {
        //        return dal.Search(userId, nickName, iFromAge, iToAge, sex);
        //    }
        //}


        /// <summary>
        /// 更新基本资料
        /// </summary>
        /// <param name="entity"></param>
        //public static void UpdateProfile(UsersInfo entity)
        //{
        //    using (UsersDAL dal = new UsersDAL())
        //    {
        //         dal.UpdateProfile(entity);
        //    }
        //}

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="identity"></param>
        //public static void ChangeSecurity(int id, string password)
        //{
        //    using (UsersDAL dal = new UsersDAL())
        //    {
        //        string oldPwd=dal.Select(id).LoginPwd;
        //        if (!password.Equals(oldPwd))
        //            throw new Exception("你输入的原始密码不正确！请重新输入！");
        //        dal.ChangeSecurity(id, password);
        //    }
        //}

        /// <summary>
        /// 更换头像
        /// </summary>
        /// <param name="faceId"></param>
        public static void ChangeFace(int id,string faceId)
        {
            using (UsersDAL dal = new UsersDAL())
            {
                dal.ChangeFace(id, faceId);
            }

        }
        /// <summary>
        /// 聊天记录
        /// </summary>
        /// <param name="fromUserId"></param>
        /// <param name="toUserId"></param>
        /// <returns></returns>
        public static string GetRecords(int fromUserId,int toUserId)
        {
            StringBuilder sb = new StringBuilder();
            List<MessagesInfo> list = null;
            using (UsersDAL dal = new UsersDAL())
            {
                list = dal.GetRecords(fromUserId,toUserId);
            }
            for (int i = list.Count-1; i >= 0;i-- )
            {
                sb.Append("[");
                sb.Append(list[i].FromUser.NickName);
                sb.Append("]");
                sb.Append("对");
                sb.Append("[");
                sb.Append(list[i].ToUser.NickName);
                sb.Append("]");
                sb.Append("说 ");
                sb.Append("[发送时间:");
                sb.Append(list[i].MessageTime.ToString());
                sb.Append("]");
                sb.Append("\r\n");
                sb.Append(list[i].Message);
                sb.Append("\r\n");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 聊天记录
        /// </summary>
        /// <param name="fromUserId"></param>
        /// <param name="toUserId"></param>
        /// <returns></returns>
        public static string GetRecords(int fromUserId, int toUserId,DateTime start,DateTime end)
        {
            StringBuilder sb = new StringBuilder();
            List<MessagesInfo> list = null;
            using (UsersDAL dal = new UsersDAL())
            {
                list = dal.GetRecords(fromUserId, toUserId,start,end);
            }
            for (int i = list.Count - 1; i >= 0; i--)
            {
                sb.Append("[");
                sb.Append(list[i].FromUser.NickName);
                sb.Append("]");
                sb.Append("对");
                sb.Append("[");
                sb.Append(list[i].ToUser.NickName);
                sb.Append("]");
                sb.Append("说 ");
                sb.Append("[发送时间:");
                sb.Append(list[i].MessageTime.ToString());
                sb.Append("]");
                sb.Append("\r\n");
                sb.Append(list[i].Message);
                sb.Append("\r\n");
            }
            return sb.ToString();
        }
    }
}
